package com.note.androidapplication;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;

import com.note.androidapplication.databinding.ActivityMain2Binding;

import io.flutter.embedding.android.FlutterFragment;

public class MainActivity2 extends FragmentActivity {

    private ActivityMain2Binding mainActivity2Binding;
    private static final String TAG = "flutter_fragment";
    private FlutterFragment flutterFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mainActivity2Binding = ActivityMain2Binding.inflate(getLayoutInflater());
        setContentView(mainActivity2Binding.getRoot());

        initFragment();
    }

    void initFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        flutterFragment = (FlutterFragment) fragmentManager.findFragmentByTag(TAG);

        if (flutterFragment == null) {
            // 入口固定，缓存的engine无法修改初始界面
            flutterFragment = FlutterFragment.withCachedEngine("my_engine").build();
            fragmentManager.beginTransaction().add(R.id.fragment_container, flutterFragment, TAG).commit();
        }
    }
}